/**
 * 
 */
package com.sys.console.cookie;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.sys.console.business.SysUserService;
import com.sys.console.monitor.MonitorLogs;
import com.sys.console.pojo.Sys_User;
import com.sys.object.common.context.Config;
import com.sys.object.common.utils.IpUtils;
import com.sys.object.common.utils.MyStringUtils;

/**
 * @author Administrator
 *
 */
public class LoginInitInterceptor extends HandlerInterceptorAdapter{

	@Autowired SysUserService sysUserService;
	
	@Override
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
		//FilterChain chain = 
		Authorized authorized = ((HandlerMethod) handler).getMethodAnnotation(Authorized.class);
		HttpSession session = request.getSession();
		Sys_User user = (Sys_User) request.getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
		String company = (String) request.getSession().getAttribute(Constant_Cookie.SESSION_COMPANY);
		String reqUrl = request.getRequestURI();
		
		if(!StringUtils.hasText(company)){
			 request.getSession().setAttribute(Constant_Cookie.SESSION_COMPANY, MyStringUtils.decodeUnicode(Config.getConfigValue("company")));
		}
		if(user==null){
			user = new Sys_User();
			user.setUid(0);
		}
		if(reqUrl.indexOf(".json")==-1){
			if(reqUrl.indexOf("welcome.do")==-1&&reqUrl.indexOf("meun.do")==-1&&reqUrl.indexOf("index.do")==-1&&reqUrl.indexOf("model.do")==-1&&reqUrl.indexOf("sysUserModuleList.do")==-1&&reqUrl.indexOf("top.do")==-1&& reqUrl.indexOf("usage.do")==-1){
				user.setLtime(new Date());
				user.setLip(IpUtils.getIpAddr(request));
				MonitorLogs.filterSaveLogs(user.getUid(), 4, null,request);
			}
		}
		boolean flag = false;
		if(user.getUid()==null||user.getUid()==0){
			if (reqUrl != null && reqUrl.endsWith(".do")) {
				user = WebUtils.getLoggedOnUser(request);
				if (user == null) {
					/* session中user为空，则返回登录页面 */
					flag = false;
					if (StringUtils.hasText(CookieUtils.getCookieByName(request,Constant_Cookie.COMPANY_COOKIE))) {
						flag = false;
					} else {
						/* 如果cookie存在,自动登录 */
						UserLogonCookieUtils.tryAutoLogin(request);
						flag = true;
					}
				} else {
					WebUtils.login(user, request);
					flag = true;
				}
			}
			if (flag) {
				//chain.doFilter(request, response);
			} else {
				request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request, response);
			}
		}
		return super.preHandle(request, response, handler);
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {
		// String url=request.getRequestURL().toString();

	}
	
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
		
	}

}
